<template>
  <div class="app-container">
    <div style="margin-bottom: 10px;">
      <!-- 导入按钮 -->
      <el-button
        @click="dialogVisible = true"
        type="primary"
        size="mini"
        icon="el-icon-download"
      >
        导入Excel
      </el-button>
      <!-- 导出按钮 -->
      <el-button
        @click="exportData"
        type="primary"
        size="mini"
        icon="el-icon-upload2"
      >
        导出Excel
      </el-button>
      <el-table :data="list" border row-key="id" lazy :load="load">
        <el-table-column label="名称" align="left" prop="name" />
        <el-table-column label="编码" prop="dictCode" />
        <el-table-column label="值" align="left" prop="value" />
      </el-table>
    </div>
    <el-dialog title="数据字典导入" :visible.sync="dialogVisible" width="30%">
      <el-form>
        <el-form-item label="请选择Excel文件">
          <el-upload
            :auto-upload="true"
            :multiple="false"
            :limit="1"
            :on-exceed="fileUploadExceed"
            :on-success="fileUploadSuccess"
            :on-error="fileUploadError"
            :action="'http://localhost:9001/admin/core/dict/import'"
            name="file"
            accept="application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
          >
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">
          取消
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getByParentId } from '@/api/core/dict'
export default {
  data() {
    return {
      dialogVisible: false, //对话框是否显示
      list: [],
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    //加载二级节点
    load(row, treeNode, reslove) {
      getByParentId(row.id).then((res) => {
        reslove(res.data.list)
      })
    },
    fetchData() {
      getByParentId(1).then((res) => {
        this.list = res.data.list
      })
    },
    fileUploadSuccess(response) {
      if (response.code === 20000) {
        this.$message.success('数据导入成功')
        this.dialogVisible = false
      } else {
        this.$message.error('数据导入失败')
      }
    },
    fileUploadError(error) {
      this.$message.error('通信失败')
    },
    fileUploadExceed() {
      this.$message.warning('只能选取一个文件')
    },
    exportData() {
      window.location.href =
        'http://localhost:9001/admin/core/dict/exportDictToExcel'
    },
  },
}
</script>
<style scoped></style>
